const express = require('express');
const path = require('path');
// const cookieParser = require('cookie-parser')
const app = express();

app.use(express.json()); // 解析json

// 假如你的客户端请求 css 是 /client/dist/css/index.css
// 这里配置 /client/dist
// 客户端配置 /css/index.css
// app.use(express.static(__dirname + '/client/dist')); //静态目录
// app.use(express.static(__dirname + '/client/dist/page')); //静态目录
app.use(express.static(path.join(__dirname, '/dist'))); // 静态目录

app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With');
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('Content-Type', 'text/html; charset=utf-8');
  res.header('X-Powered-By', ' 3.2.1');
  next();
});

// 主页
app.get('/', (req, res) => {
  res.sendfile('index.html');
});

app.post('/login', (req, res) => {
  console.log(req.body);
  console.log(JSON.stringify(req.body));
  res.redirect('/login.html');
});

const port = 3000;
const hostname = '127.0.0.1';
app.listen(port, hostname, function (result) {
  console.log(`Server is run on http://${hostname}:${port}`);
});
